/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.thienloc.omap.entity;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.List;
import javax.persistence.*;
import javax.validation.constraints.Size;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;

/**
 *
 * @author GiangLT
 */
@Entity
@Table(name = "PERMISSION", catalog = "")
@XmlRootElement
@NamedQueries({
    @NamedQuery(name = "Permission.findAll", query = "SELECT p FROM Permission p"),
    @NamedQuery(name = "Permission.findById", query = "SELECT p FROM Permission p WHERE p.id = :id"),
    @NamedQuery(name = "Permission.findByAction", query = "SELECT p FROM Permission p WHERE p.action = :action"),
    @NamedQuery(name = "Permission.findByUserId", query = "SELECT p FROM Permission p INNER JOIN p.omapUserList user WHERE user.userId = :userId"),
    @NamedQuery(name = "Permission.findByRoleId", query = "SELECT p FROM Permission p INNER JOIN p.omapRoleList role WHERE role.roleId = :roleId"),
    @NamedQuery(name = "Permission.findByResourceId", query = "SELECT p FROM Permission p INNER JOIN p.resourceId resource WHERE resource.id = :resourceId")
})
public class Permission implements Serializable {

    private static final long serialVersionUID = 1L;
    // @Max(value=?)  @Min(value=?)//if you know range of your decimal fields consider using these annotations to enforce field validation
    @Id
    @Basic(optional = false)
    @Column(name = "ID")
    private BigDecimal id;
    @Size(max = 10)
    @Column(name = "ACTION")
    private String action;
    @ManyToMany(mappedBy = "permissionList")
    private List<OmapRole> omapRoleList;
    @ManyToMany(mappedBy = "permissionList")
    private List<OmapUser> omapUserList;
    @JoinColumn(name = "RESOURCE_ID", referencedColumnName = "ID")
    @ManyToOne
    private OmapResource resourceId;

    public Permission() {
    }

    public Permission(BigDecimal id) {
        this.id = id;
    }

    public BigDecimal getId() {
        return id;
    }

    public void setId(BigDecimal id) {
        this.id = id;
    }

    public String getAction() {
        return action;
    }

    public void setAction(String action) {
        this.action = action;
    }

    @XmlTransient
    public List<OmapRole> getOmapRoleList() {
        return omapRoleList;
    }

    public void setOmapRoleList(List<OmapRole> omapRoleList) {
        this.omapRoleList = omapRoleList;
    }

    @XmlTransient
    public List<OmapUser> getOmapUserList() {
        return omapUserList;
    }

    public void setOmapUserList(List<OmapUser> omapUserList) {
        this.omapUserList = omapUserList;
    }

    public OmapResource getResourceId() {
        return resourceId;
    }

    public void setResourceId(OmapResource resourceId) {
        this.resourceId = resourceId;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Permission)) {
            return false;
        }
        Permission other = (Permission) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "com.thienloc.omap.entity.Permission[ id=" + id + " ]";
    }

    public String getUserListAsHtmlString() {
        StringBuilder sb = new StringBuilder();
        if (omapUserList != null) {
            for (OmapUser omapUser : omapUserList) {
                sb.append(omapUser.getUsername());
                sb.append("\r\n");
            }
        }
        return sb.toString();
    }

    public String getRoleListAsHtmlString() {
        StringBuilder sb = new StringBuilder();
        if (omapRoleList != null) {
            for (OmapRole omapRole : omapRoleList) {
                sb.append(omapRole.getRoleName());
                sb.append("\r\n");
            }
        }
        return sb.toString();
    }
}
